//
//  SuggestViewController.m
//  WaywardPoint_Code
//
//  Created by 韩盼盼 on 15/7/24.
//  Copyright (c) 2015年 易网新科技. All rights reserved.
//

#import "SuggestViewController.h"
#import "AppDelegate.h"


@interface SuggestViewController ()<UITextViewDelegate,HeadBackView,UITextFieldDelegate>
{
    UITextView *sugguests;
    UITextField *userQQ;
    UILabel *label;
    UIButton *backBtn;
    UITextField *sugg;
    UIButton *surebtn;
}
@end

@implementation SuggestViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed=YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = COLOR_EVERYVIEWBG;
    self.title = @"意见反馈";

    HeadBackView *headview=[HeadBackView headbackView:@"意见反馈"andrightBtn:nil];
    headview.delegate=self;
    [self.view addSubview:headview];
    
    [self showUI];
}

- (void)showUI
{
    sugguests=[[UITextView alloc]initWithFrame:CGRectMake(20, 80, ScreenW-40, 130)];
    sugguests.delegate=self;
    sugguests.font=[UIFont systemFontOfSize:Font_SIZE_14+2];
    sugguests.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:sugguests];
    sugguests.textColor = [BaseViewController colorWithHexString:@"#cbcbcb"];
   
    //textview提示文字
    label = [[UILabel alloc]initWithFrame:CGRectMake(10, 3, ScreenW-60, 60)];
    label.enabled = NO;
    
    label.text = @"请简单描述你所遇到的问题和建议，还可以添加QQ或微信账号与我们及时沟通哟~";
    label.font =  [UIFont systemFontOfSize:Font_SIZE_14];
    label.numberOfLines=4;
    label.adjustsFontSizeToFitWidth = YES;
    label.textColor = [BaseViewController colorWithHexString:@"#384056"];
    [sugguests addSubview:label];
    
    userQQ=[[UITextField alloc]initWithFrame:CGRectMake(20, sugguests.y+150, ScreenW-40, 30)];
    userQQ.leftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 30)];
    userQQ.leftViewMode=UITextFieldViewModeAlways;
    userQQ.backgroundColor = [UIColor whiteColor];
    userQQ.delegate=self;
    userQQ.returnKeyType = UIReturnKeyDone;
    [self.view addSubview:userQQ];
    userQQ.textColor = [BaseViewController colorWithHexString:@"#384056"];
    userQQ.keyboardType = UIKeyboardTypeNumberPad;
    
    [userQQ addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    userQQ.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入您的手机号码" attributes:@{NSForegroundColorAttributeName:[BaseViewController colorWithHexString:@"#384056"]}];
    
    userQQ.font = [UIFont systemFontOfSize:30*PROPORTION_WIDTH];
    
    UILabel *tishi=[[UILabel alloc]initWithFrame:CGRectMake(20, userQQ.y+40, ScreenW-40, 45)];
    tishi.text=@"上述内容仅对【MR帮】工作人员可见，可帮助我们更快的与你沟通并解决问题。";
    tishi.numberOfLines=2;
    tishi.font=[UIFont systemFontOfSize:30*PROPORTION_WIDTH];
    tishi.textColor = [BaseViewController colorWithHexString:@"#384056"];
    [self.view addSubview:tishi];
    surebtn=[[UIButton alloc]initWithFrame:CGRectMake(20, tishi.y+60, ScreenW-40, 40)];
    [surebtn setTitle:@"发送" forState:UIControlStateNormal];
    [surebtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    surebtn.backgroundColor = COLOR_UNBUTTON;
//    [surebtn setBackgroundImage:[UIImage imageNamed:@"button_submit"] forState:UIControlStateNormal];
    [surebtn addTarget:self action:@selector(suggBtnEventRequest) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:surebtn];
}

-(void)viewWillAppear:(BOOL)animated
{
    sugguests.text=@"";
    label.hidden=NO;
    userQQ.text=@"";
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField==userQQ) {
        [self suggBtnEventRequest];
    }
    return YES;
}


- (void)textFieldDidChange:(UITextField *)textField
{
    if (userQQ == textField) {
        if (sugguests.text.length>0) {
            
            if (userQQ.text.length>10) {
              textField.text = [userQQ.text substringToIndex:11];
                surebtn.userInteractionEnabled=YES;
                surebtn.backgroundColor = COLOR_BUTTON;
            }
            else
            {
                surebtn.backgroundColor = COLOR_UNBUTTON;
                surebtn.userInteractionEnabled=NO;
            }
        }
        else
        {
            surebtn.backgroundColor = COLOR_UNBUTTON;
            surebtn.userInteractionEnabled=NO;
        }
    }
}

-(void)suggBtnEventRequest
{
    [self.view endEditing:YES];
    // 1.用户名
    NSString *sugguestsinfo = sugguests.text;
    if (sugguestsinfo.length == 0) {
        surebtn.userInteractionEnabled=YES;
        surebtn.backgroundColor = COLOR_BUTTON;

        [UIView showAlertMsg:@"请输入反馈内容"];
        return;
    }
    
    if (![BaseViewController isMobileNumber_str:userQQ.text]) {
        [UIView showAlertMsg:@"请输入正确的手机号"];
        return;
    }
    
    // 2.密码
    NSString *linktype = userQQ.text;
    
    if (linktype.length == 0) {
        surebtn.userInteractionEnabled=YES;
        surebtn.backgroundColor = COLOR_BUTTON;

        [UIView showAlertMsg:@"请输入联系方式" andShowBgColor:[UIColor blackColor] andLabelTectColor:[UIColor whiteColor]];
        
        return;
    }
    
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    NetworkStatus status = [reach currentReachabilityStatus];
    NSString *urlstr=[NSString stringWithFormat:@"%@suggest/add",MRAPI];
   
    if (![UIView NetFromStatus:status]) {
        NSLog(@"没有网络");
        [UIView showAlertMsg:@"网络连接已断开" andShowBgColor:[UIColor blackColor] andLabelTectColor:[UIColor whiteColor]];
        
        return;
    }else
    {
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"memberid"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"memberid"];
        params[@"content"] = sugguestsinfo;
        params[@"contact"] = linktype;
        
        [MBProgressHUD showMessage:@"正在提交反馈信息..." toView:self.view];
        [[HttpRequest shardWebUtil]postNetworkRequestURLString:urlstr andParas:params andTransferGottenData:^(id obj, NSError *error) {
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            surebtn.userInteractionEnabled=YES;
            
            if (error == nil) {
                [UIView showAlertMsg:@"反馈成功" andShowBgColor:[UIColor blackColor] andLabelTectColor:[UIColor whiteColor]];
                [self.navigationController popViewControllerAnimated:YES];
            }
            else
            {
                [UIView showAlertMsg:@"网络请求超时" andShowBgColor:[UIColor blackColor] andLabelTectColor:[UIColor whiteColor]];
            }
        }];
    }
}

#pragma mark -- UITextViewDelegate

- (void) textViewDidChange:(UITextView *)textView{
    if ([textView.text length] == 0) {
        [label setHidden:NO];
    }else{
        [label setHidden:YES];
    }
}

- (void)btnback
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self view] endEditing:YES];
}
@end
